package com.registration.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.registration.Annotation.OperateLog;
import com.registration.bean.CodeBean;
import com.registration.bean.UserBean;
import com.registration.service.UserService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/loginByPassword")
    @OperateLog("用户密码登录")
    public JsonResponse loginByPassword(String phone, String password) {
        JsonResponse json = new JsonResponse();
        if (password == null || password.trim().isEmpty()) {
            json.setCode(0);
            json.setMsg("密码不能为空");
            return json;
        }
        Integer result;
        try {
            result = userService.loginByPassword(phone, password);
            if(result == 0){
                StpUtil.login(phone);
                json.setCode(0);
                json.setMsg("登录成功");
                json.setData(userService.selectUserByPhone(phone));
                json.setSaTokenInfo(StpUtil.getTokenInfo());
            } else {
                json.setCode(1);
                json.setMsg("账号或密码错误");
            }
        } catch (Exception e) {
            json.setCode(0);
            if(e.getMessage().equals("该用户已被禁用")){
                json.setMsg("该用户已被禁用");
            } else if (e.getMessage().equals("该用户不存在")) {
                json.setMsg("该用户不存在");
            } else {
                json.setMsg("账号或密码错误");
            }
            e.printStackTrace();
        }
        return json;
    }

    @GetMapping("/loginByCode")
    @OperateLog("用户验证码登录")
    public JsonResponse loginByCode(String phone,Integer code) {
        JsonResponse json = new JsonResponse();
        if (code<100000||code>999999) {
            json.setCode(0);
            json.setMsg("验证码无效");
            return json;
        }
        try {
            CodeBean codeBean = userService.loginByCode(phone,code);
            if(codeBean==null) {
                json.setCode(1);
                json.setMsg("验证码不匹配");
            }else if(codeBean.getCode_id()==0){
                json.setCode(2);
                json.setMsg("该手机号未注册账号,已为您自动注册");
                register(new UserBean(phone,null,1,new Date(), code));
            } else{
                StpUtil.login(phone, "mobile");
                json.setCode(0);
                json.setMsg("登录成功");
                json.setData(userService.selectUserByPhone(phone));
                json.setSaTokenInfo(StpUtil.getTokenInfo());
                System.out.println(StpUtil.getTokenInfo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setCode(1);
            json.setMsg("验证码不匹配");
            if(e.getMessage().equals("该用户已被禁用")){
                json.setMsg("该用户已被禁用");
            }
        }
        return json;
    }

    @PostMapping("/register")
    @OperateLog("用户注册")
    public JsonResponse register(@RequestBody UserBean userBean) {
        JsonResponse json = new JsonResponse();
        Integer result = 1;
        try {
            result = userService.register(userBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        json.setCode(result);
        if(result==0)
            json.setMsg("注册成功");
        else if (result==2)
            json.setMsg("手机号已存在");
        else
            json.setMsg("验证码错误");
        return json;
    }

    @PostMapping("/sendCode")
    @OperateLog("发送验证码")
    //生成验证码，插入数据库并发送验证码
    public JsonResponse sendCode(@RequestBody CodeBean codeBean) {
        JsonResponse json = new JsonResponse();
        try {
            Integer code = userService.sendCode(codeBean.getPhone());
            json.setCode(0);
            json.setMsg("验证码发送成功");
            json.setData(code);
        } catch (Exception e) {
            e.printStackTrace();
            json.setCode(1);
            json.setMsg("验证码发送失败");
        }
        return json;
    }

    @PostMapping("/resetPassword")
    @OperateLog("重置密码")
    //重置密码
    public JsonResponse resetPassword(@RequestBody UserBean userBean) {
        JsonResponse json = new JsonResponse();
        Integer result = 1;
        try {
            result = (userService.resetPassword(userBean.getPhone(), userBean.getPassword(), userBean.getCode())==1)?0:1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        json.setCode(result);
        if(result==0)
            json.setMsg("密码重置成功");
        else
            json.setMsg("密码重置失败");
        return json;
    }

    @PutMapping("/updateUser")
    @OperateLog("更新用户信息")
    //更新用户信息
    public JsonResponse updateUser(@RequestBody UserBean userBean) {
        JsonResponse json = new JsonResponse();
        Integer result = (userService.updateUser(userBean)==1) ?0:1;
        json.setCode(result);
        if(result==0)
            json.setMsg("更新成功");
        else
            json.setMsg("更新失败");
        return json;
    }

    @PutMapping("/changePhone")
    @OperateLog("更新手机号")
    public JsonResponse changePhone(@RequestBody UserBean userBean) {
        JsonResponse json = new JsonResponse();
        Integer result = userService.changePhone(userBean);
        if(result==0) {
            json.setCode(0);
            json.setMsg("修改成功");
        } else if (result==2) {
            json.setCode(2);
            json.setMsg("该手机号已绑定用户，请先解绑");
        } else {
            json.setCode(1);
            json.setMsg("修改失败");
        }
        return json;
    }


}
